package cn.itsource.controller;

import cn.itsource.domain.Course;
import cn.itsource.mapper.CourseMapper;
import cn.itsource.query.Query;
import cn.itsource.service.CourseService;
import cn.itsource.util.AjaxResult;
import cn.itsource.util.DataListener;
import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @Autowired
    private CourseMapper courseMapper;

    //注意前端地址：http://localhost:8080/course - get
    @GetMapping
    public List<Course> findAll() {
        return courseService.findAll();
    }


    //注意前端地址：http://localhost:8080/course/1 - get
    @GetMapping("/{id}")
    public Course findOne(@PathVariable("id") Long id) {
        return courseService.findOne(id);
    }


   //注意前端地址：http://localhost:8080/course - put - {}
   @PutMapping
   public AjaxResult addorUpdate(@RequestBody Course course){
       try {
           if(course.getId()==null){//添加操作
               courseService.add(course);
           }else{
               courseService.update(course);
           }
           //如果成功
           return new AjaxResult();
       } catch (Exception e) {
           e.printStackTrace();
           return new AjaxResult(false,"网络繁忙,请稍后重试");
       }
   }

    //注意前端地址：http://localhost:8080/course/1 - delete
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")Long id){
        try {
            courseService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }
    //分页+模糊查询
   @PostMapping
   public PageInfo<Course> pageQuery(@RequestBody Query query){

        //前端接收参数：total，list
        return courseService.pageQuery(query);
   }

    //批量删除
   @PatchMapping
   public AjaxResult patchdel(@RequestBody Long[] ids){
       try {
           courseService.patchdel(ids);
           return new AjaxResult();
       } catch (Exception e) {
           e.printStackTrace();
           return new AjaxResult(false,"网络繁忙,请稍后重试");
       }
   }

    //导出文件
    @PatchMapping("/exportFile")
    public AjaxResult exportFile(@RequestBody Long[] ids){
        try {
            courseService.exportFile(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"网络繁忙,请稍后重试");
        }
    }

    //选取EXCEL文件导入
    @PostMapping("/updown")
    public AjaxResult  upload(@RequestPart("files") MultipartFile file) throws IOException {
        EasyExcel.read(file.getInputStream(), Course.class, new DataListener(courseMapper)).sheet().doRead();
        return AjaxResult.me();
    }


}
